/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.entity;

import exceptions.IdentifiantInvalide;
import exceptions.InstanceInvalide;
import java.util.Set;
import javax.ejb.Remote;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author François
 */
@Entity
@Remote(ProductStore.class)
@Table(name = "productStore")
@NamedQueries({ @NamedQuery(name = "ProductStore.getAll", query = "SELECT ps FROM ProductStore ps") })
public class ProductStore implements ProductStoreRemote {
    /**
     * Default serial version UID.
     */
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "reference_productStore")
    private Long reference;

    @Column(name = "name_productStore", length = 255)
    private String name;

    @ManyToOne(fetch = FetchType.EAGER, targetEntity = Account.class)
    @JoinColumn(name = "account_productStore")
    private AccountRemote account;

    @OneToMany(mappedBy = "productStore", fetch = FetchType.EAGER, targetEntity = Product.class)
    private Set<ProductRemote> products;
    

    public ProductStore() {
    }
    
    public ProductStore(Long reference, String name,
	    AccountRemote account,
	    Set<ProductRemote> products) {
	super();
	this.reference = reference;
	this.name = name;
	this.account = account;
	this.products = products;
    }
    
    public static void verifierReference(Long reference)
	    throws IdentifiantInvalide {
	/*
	 * Identifiant renseigné
	 */
	if (reference == null) {
	    throw new IdentifiantInvalide(
		    "L'identifiant du Magasin est obligatoire. null");
	}
    }
    
    public static void verifierInstance(ProductStoreRemote productStore)
	    throws InstanceInvalide, IdentifiantInvalide {
	if (productStore != null) {
	    /*
	     * Vérification référence
	     */
	    ProductStore.verifierReference(productStore.getReference());

	    /*
	     * Vérification compte
	     */
	    if (productStore.getAccount() == null) {
		throw new InstanceInvalide("Le Compte du Magasin est nul");
	    }
	} else {
	    throw new InstanceInvalide("L'instance de Magasin est nulle");
	}
    }

    public Long getReference() {
        return reference;
    }

    public void setReference(Long reference) {
        this.reference = reference;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public AccountRemote getAccount() {
        return this.account;
    }

    public void setAccount(AccountRemote account) {
        this.account = (Account) account;
    }

    public Set<ProductRemote> getProducts() {
	return this.products;
    }

    public void setProducts(Set<ProductRemote> products) {
	this.products = products;
    }

    @Override
    public int hashCode() {
	int prime = 31;
	int result = 1;
	result = prime * result
		+ ((account == null) ? 0 : account.getIban().hashCode());
	result = prime * result + ((name == null) ? 0 : name.hashCode());
	result = prime * result
		+ ((products == null) ? 0 : products.hashCode());
	result = prime * result
		+ ((reference == null) ? 0 : reference.hashCode());
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj) {
	    return true;
	}
	if (obj == null) {
	    return false;
	}
	if (!(obj instanceof ProductStore)) {
	    return false;
	}
	ProductStore other = (ProductStore) obj;
	if (account == null) {
	    if (other.account != null) {
		return false;
	    }
	} else if (!account.getIban().equals(other.account.getIban())) {
	    return false;
	}
	if (name == null) {
	    if (other.name != null) {
		return false;
	    }
	} else if (!name.equals(other.name)) {
	    return false;
	}
	if (products == null) {
	    if (other.products != null) {
		return false;
	    }
	} else if (!products.equals(other.products)) {
	    return false;
	}
	if (reference == null) {
	    if (other.reference != null) {
		return false;
	    }
	} else if (!reference.equals(other.reference)) {
	    return false;
	}
	return true;
    }

    @Override
    public String toString() {
	return "Magasin [reference=" + reference + ", name=" + name +
                ", account=" + account.getIban() + ", products="
		+ products + "]";
    }
    
}
